package com.jgp.third.wx.controller;

import com.jgp.sys.controller.JGPBackController;
import com.jgp.third.wx.model.WxUseFor;
import com.jgp.third.wx.repository.WxUseForRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2018/6/3
 */
@Controller
@RequestMapping("third/wx/wxConfigController")
public class WxConfigController extends JGPBackController {

    @Autowired
    private WxUseForRepository useForRepository;

    @RequestMapping("/queryUseFor")
    public String queryUseFor(){
        return page("queryUseFor");
    }


    @RequestMapping("/addUseFor")
    public String addUseFor(){
        return page("editUseFor");
    }

    @RequestMapping("/editUseFor/{id}")
    public String editUseFor(@PathVariable Long id,Model model){
        reForm(model, "fdata", useForRepository.read(id));
        return page("editUseFor");
    }


    @RequestMapping("/addConfig")
    public String addConfig(){
        return page("editConfig");
    }

    @RequestMapping("/generateUrl")
    public String generateUrl(Long useForId, HttpServletRequest request,Model model) {
        WxUseFor useFor = useForRepository.read(useForId);
        String authBack = request.getScheme() + "://" + useFor.getDomain() + request.getContextPath() +
                "/third/wx/wxInterfaceController/toAuth?useForId="+useForId+"&domain="+useFor.getDomain();
        model.addAttribute("url",authBack);
        return page("generateUrl");
    }
}
